package com.zsq.common.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsq.common.utils.DateUtil;
import com.zsq.common.utils.MyUtils;

/**
 * @author zsq
 * 时间 2020/2/4 15:18
 **/
public class SqBaseServiceImpl<M extends SqBaseMapper<T>, T extends BaseEntity>
        extends ServiceImpl<M, T>
        implements SqBaseService<T> {

    @Override
    public T selectOneByUuid(String uuid) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uuid", uuid);

        T entity = this.getOne(queryWrapper);

        return entity;
    }

    @Override
    public boolean updateByUuid(T entity) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uuid", entity.getUuid());

        //设置当前时间
        String currentDateString = DateUtil.getCurrentDateString(); //当前时间
        entity.setUpdateDateTime(currentDateString);
        boolean b = this.update(entity, queryWrapper);

        return b;
    }

    @Override
    public boolean insert(T entity) {
        String currentDateString = DateUtil.getCurrentDateString(); //当前时间
        entity.setCreateDateTime(currentDateString);

        boolean b = this.save(entity);
        return b;
    }

    @Override
    public boolean insertAutoUuid(T entity) {
        String currentDateString = DateUtil.getCurrentDateString(); //当前时间
        entity.setCreateDateTime(currentDateString);
        entity.setUuid(MyUtils.uuid());

        boolean b = this.save(entity);
        return b;
    }

    @Override
    public boolean deleteByUuid(String uuid) {
        if(uuid == null){
            return false;
        }
        QueryWrapper<T> wrapper= new QueryWrapper<>();
        wrapper.in("uuid", uuid.split(","));
        boolean b = this.remove(wrapper);
        return b ;
    }

}
